﻿using System;
using System.Collections.Generic;
using System.Text;
using FeCMS.Common.Interface;
using FeCMS.Common;
using FeCMS.Common.Enum;

namespace FeCMS.CommonFunction
{
    /// <summary>
    ///     静态方法
    /// </summary>
    public  class Function
    {
        /// <summary>
        ///     合并错误代码，
        ///     把错误模板跟错误原因合并
        /// </summary>
        /// <param name="templateErrorCode">错误模板</param>
        /// <param name="reasonErrorCode">错误原因</param>
        /// <returns></returns>
        public static IErrorCode MergeErrorCode(IErrorCode templateErrorCode, IErrorCode reasonErrorCode)
        {
            return MergeErrorCode(
                templateErrorCode, 
                reasonErrorCode, 
                string.Concat(
                    templateErrorCode.LongMessage, 
                    reasonErrorCode.LongMessage
                    )
            );
        }

        /// <summary>
        ///     合并错误代码，
        ///     把错误模板跟错误原因合并
        /// </summary>
        /// <param name="templateErrorCode">错误模板</param>
        /// <param name="reasonErrorCode">错误原因</param>
        /// <param name="args">替换参数</param>
        /// <returns></returns>
        public static IErrorCode MergeErrorCode(IErrorCode templateErrorCode, IErrorCode reasonErrorCode, params string[] args)
        {
            try
            {
                return MergeErrorCode(
                    templateErrorCode,
                    reasonErrorCode,
                    string.Concat(
                        string.Format(templateErrorCode.LongMessage, args),
                        string.Format(reasonErrorCode.LongMessage, args)
                    ));
            }
            catch (Exception ex)
            {
                var error = ErrorCode.ObjectConversionError;
                error.ShortMessage = ex.Message;
                return error;
            }
        }


        /// <summary>
        ///     合并错误代码，
        ///     把错误模板跟错误原因合并
        /// </summary>
        /// <param name="templateErrorCode">错误模板</param>
        /// <param name="reasonErrorCode">错误原因</param>
        /// <param name="args">替换参数</param>
        /// <returns></returns>
        public static IErrorCode MergeErrorCode(IErrorCode templateErrorCode, IErrorCode reasonErrorCode, string longMessage)
        {
            try
            {
                return new ErrorCode(
                    new string(CombineCode(templateErrorCode.Code.ToCharArray(), reasonErrorCode.Code.ToCharArray())),
                    templateErrorCode.ShortMessage,
                    longMessage,
                    ErrorLevel.Site
                );
            }
            catch (Exception ex)
            {
                var error = ErrorCode.ObjectConversionError;
                error.ShortMessage = ex.Message;
                return error;
            }
        }


        /// <summary>
        ///     单个汉字转拼音
        /// </summary>
        /// <param name="cnChar"></param>
        /// <returns></returns>
        public static string ChangeToSpell(string cnChar)
        {
            byte[] arrCN = Encoding.Default.GetBytes(cnChar);
            if (arrCN.Length > 1)
            {
                int area = (short)arrCN[0];
                int pos = (short)arrCN[1];
                int code = (area << 8) + pos;
                int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
                for (int i = 0; i < 26; i++)
                {
                    int max = 55290;
                    if (i != 25) max = areacode[i + 1];
                    if (areacode[i] <= code && code < max)
                    {
                        return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
                    }
                }
                return "*";
            }
            else return cnChar;
        }

        #region 私有方法

        /// <summary>
        ///     合并错误Code
        /// </summary>
        /// <param name="a">字符数组A</param>
        /// <param name="b">字符数组B</param>
        /// <returns></returns>
        private static char[] CombineCode(char[] a, char[] b)
        {
            /*
            * 两组Code合并成新的Code
            * 规则：两组字符对比，遇0合并
            * 例子  ：0x00010000 0x00000001  
            * 合并后：0x00010001
            */
            char[] code = new char[10];
            for (int i = 0; i < 10; i++)
            {
                code[i] = a[i] != '0' ? a[i] : b[i];
            }
            return code;
        }

        #endregion
    }
}
